package com.itc.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.poi.ExcelUtil;
import com.itc.common.log.annotation.Log;
import com.itc.common.log.enums.BusinessType;
import com.itc.common.redis.service.RedisService;
import com.itc.common.security.annotation.InnerAuth;
import com.itc.common.security.annotation.RequiresPermissions;
import com.itc.system.api.domain.SysLogininfor;
import com.itc.system.service.SysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 * 
 * @author itc
 */
@RestController
@RequestMapping("/loginLog")
@Api(value = "sysLoginLog", tags = "登录日志管理模块")
public class SysLogininforController
{
    @Autowired
    private SysLogininforService logininforService;

    @Resource
    private RedisService redisService;

//    @RequiresPermissions("system:logininfor:list")
    @PostMapping("/page")
    @ApiOperation(value = "登录日志分页获取")
    public R<Page<SysLogininfor>> getPage(@RequestBody SysLogininfor logininfor)
    {
        Page<SysLogininfor> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(logininfor.getSize());
        page.setCurrent(logininfor.getCurrent());
        Page<SysLogininfor> resultPage = logininforService.page(page,new LambdaQueryWrapper<>(logininfor));
        return R.ok(resultPage);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    @ApiOperation(value = "登录日志导出")
    public void export(HttpServletResponse response, @RequestBody SysLogininfor logininfor)
    {
        List<SysLogininfor> list = logininforService.list(new LambdaQueryWrapper<>(logininfor));
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    @InnerAuth
    @PostMapping
    public R add(@RequestBody SysLogininfor logininfor)
    {
        return R.ok(logininforService.save(logininfor));
    }
}
